package ua.at.tsvetkov.samizdat.services;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.AsyncTaskGenre;
import ua.at.tsvetkov.util.Log;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class SrvGenres extends Service {

	private IBinder					binder;
	private NotificationManager	notificationManager;

	private int							NOTIFICATION	= R.string.SRV_GENRES_STARTED;

	private AsyncTaskGenre			asyncTaskGenres;

	public class LocalBinder extends Binder {
		SrvGenres getService() {
			return SrvGenres.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.v(getString(R.string.SRV_GENRES_BINDED));

		return binder;
	}

	@Override
	public void onCreate() {
		binder = new LocalBinder();
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public void onStart(Intent intent, int startid) {
		Log.v(getString(R.string.SRV_GENRES_STARTED));

		Intent broadcastIntent = new Intent(Const.BROADCAST_MESSAGE).putExtra(Const.SERVICE_GENRES_STARTED, Const.SERVICE_GENRES_STARTED);
		sendBroadcast(broadcastIntent);

		asyncTaskGenres = new AsyncTaskGenre(this);
		asyncTaskGenres.execute();

	}

	@Override
	public void onDestroy() {
		Log.v(getString(R.string.SRV_GENRES_STOPED));

		notificationManager.cancel(NOTIFICATION);
		if (!asyncTaskGenres.isEndWithError()) {
			Intent broadcastIntent = new Intent(Const.BROADCAST_MESSAGE).putExtra(Const.SERVICE_GENRES_STOPED, Const.SERVICE_GENRES_STOPED);
			sendBroadcast(broadcastIntent);
		}
	}
}
